import requests
import execjs
import time


# 定义请求信息
url = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token=45165d1641bd55361205374cbfab53d7'
headers = {
    'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'
}

with open(r'E:\Python\vscode\Crawlers\js逆向\03网易云音乐_评论\params_a.js','r',encoding='utf-8') as f:
    js = execjs.compile(f.read())

i4m1 = {
    "rid": "A_PL_0_514947114",
    "threadId": "A_PL_0_514947114",
    "pageNo": "1",
    "pageSize": "20",
    "cursor": "-1",
    "offset": "0",
    "orderType": "1",
    "csrf_token": "45165d1641bd55361205374cbfab53d7"
}

def func_req():
    resp = requests.post(url,headers=headers,data=data)
    json_data = resp.json()
    cursor = json_data['data']['cursor']
    comments = json_data['data']['comments']
    for i in comments:
        print(i['content'])
    print('-------------------------------------------------------')
    return cursor

if __name__ == '__main__':
    # 先尝试读取十页的评论数据
    for i in range(10):
        if i == 0:
            data = js.call('get_key',i4m1)
            print(data)
            cursor = func_req()
            break
        else:
            i4m1['pageNo'] = str(i+1)
            i4m1['cursor'] = cursor
            data = js.call('get_key',i4m1)
            cursor = func_req()
